//
//  VMCPagedListDomain.m
//  vmcshop
//
//  Created by bigknife on 14-10-10.
//  Copyright (c) 2014年 idongler. All rights reserved.
//

#import "VMCPagedListDomain.h"


@implementation VMCPagedListDomain
+(id) instanceFromListApiInfo:(NSDictionary *)info withItemsMapBlock:(id(^)(id item, NSInteger idx))block{
	return [self instanceFromListApiInfo:info
						   withItemsName:@"items"
							pageInfoName:@"pageInfo"
							 andMapBlock:block];
}
+(id) instanceFromListApiInfo:(NSDictionary *)info withItemsName:(NSString *)itemsName
				 pageInfoName:(NSString *)pageInfoName
				  andMapBlock:(id(^)(id item, NSInteger idx))block{
	VMCPagedListDomain *list = [VMCPagedListDomain new];
	
	if (info && info != (id)[NSNull null]) {
		NSArray *items = [info objectForKey:itemsName];
		if (block) {
			items = [items mapWithBlock:block];
		}
		//list.items = items;
		list.items = [[NSMutableArray alloc]initWithArray:items];
		
		NSDictionary *pageInfo  = [info objectForKey:pageInfoName];
		if (pageInfo && pageInfo != (id)[NSNull null]) {
			list.pageInfo = [[VMCPageInfo alloc]initWithDictionary:pageInfo];
		}
	}
	
	return list;
}
-(void)appendFromListApiInfo:(NSDictionary *)info withItemsMapBlock:(id (^)(id item, NSInteger idx))block{
	return [self appendFromListApiInfo:info
						 withItemsName:@"items"
						  pageInfoName:@"pageInfo"
						   andMapBlock:block];
}
-(void)appendFromListApiInfo:(NSDictionary *)info
			   withItemsName:(NSString *)itemsName
				pageInfoName:(NSString *)pageInfoName
		   andMapBlock:(id (^)(id item, NSInteger idx))block{
	if (info && info != (id)[NSNull null]) {
		NSArray *items1 = [info objectForKey:itemsName];
		if (block) {
			items1 = [items1 mapWithBlock:block];
		}
	

	self.items	= [self.items appendArray:items1];
		
		
		NSDictionary *pageInfo  = [info objectForKey:pageInfoName];
		if (pageInfo && pageInfo != (id)[NSNull null]) {
			self.pageInfo = [[VMCPageInfo alloc]initWithDictionary:pageInfo];
		}
	}
}
@end
